package com.dingreading.cloud.web.service.impl;

import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.web.entity.table.NjTicketSellerTableDef;
import com.dingreading.cloud.web.entity.NjTicketSeller;
import com.dingreading.cloud.web.mapper.NjTicketSellerMapper;
import com.dingreading.cloud.web.service.NjTicketSellerService;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.springframework.stereotype.Service;

/**
 *  服务层实现。
 *
 * @author Administrator
 * @since 2024-12-23
 */
@Service
public class NjTicketSellerServiceImpl extends BaseServiceImpl<NjTicketSellerMapper, NjTicketSeller, NjTicketSellerTableDef> implements NjTicketSellerService {

    @Override
    protected NjTicketSellerTableDef getTable() {
        return NjTicketSellerTableDef.njTicketSeller;
    }

    @Override
    public int getMaxSellerCode(String corpUid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.sellerCode))
                .from(table)
                .where(table.corpUid.eq(corpUid));

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);
        return NepUtil.nullToZero(integer);
    }

    @Override
    public boolean updateInfo(String corpUid, String sellerCode, String sellerName, String sellerPhone) {
        return UpdateChain.of(NjTicketSeller.class)
                .set(table.sellerName, sellerName)
                .set(table.sellerPhone, sellerPhone)
                .set(table.bindTime, DateUtil.getNowDatetime())
                .where(table.corpUid.eq(corpUid).and(table.sellerCode.eq(sellerCode)))
                .update();
    }

    @Override
    public NjTicketSeller getSeller(String corpUid, String sellerCode) {
        return getMapper().selectOneByCondition(table.corpUid.eq(corpUid).and(table.sellerCode.eq(sellerCode)));
    }
}
